package com.wbp.cms.core.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.wbp.cms.client.common.domain.DictionaryDO;
import com.wbp.cms.client.common.query.DictionaryQuery;
import com.wbp.cms.client.common.service.DictionaryService;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.core.dao.DictionaryDAO;


/**
 * @Description:
 * @Department:
 * @author andy.lhc
 * @date 2016年3月9日 下午2:26:48
 */
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {

  /**
   * 
   */
  private static final long serialVersionUID = -9191465657781795419L;

  @Resource
  private DictionaryDAO   dictionaryDAO ;

  @Override
  public  Long  addDictionary(DictionaryDO dictionaryDO) {
    Preconditions.checkNotNull(dictionaryDO, "dictionaryDO:"+dictionaryDO+",不能为null") ;
    return  this.dictionaryDAO.insert(dictionaryDO)  ;
  }
   
  @Override
  public  boolean  modifyDictionary(DictionaryDO dictionaryDO) {
    Preconditions.checkNotNull(dictionaryDO, "dictionaryDO:"+dictionaryDO+",不能为null") ;
    return  this.dictionaryDAO.update(dictionaryDO) > 0  ;
  }
   
  @Override
  public  DictionaryDO   loadDictionary(Long id) {
    Preconditions.checkNotNull(id, "dictionaryId:"+id+",不能为空") ;
    
    return  this.dictionaryDAO.load(id) ;
  }
   
  @Override
  public Result<List<DictionaryDO>> queryDictionary(DictionaryQuery dictionaryQuery) {
    Preconditions.checkNotNull(dictionaryQuery, "dictionaryQuery:"+dictionaryQuery+",不能为null") ;
    
    int count  = this.dictionaryDAO.queryCount(dictionaryQuery) ;
    List<DictionaryDO> list = null ;
    if(count > 0){
        list = this.dictionaryDAO.query(dictionaryQuery) ;
    }
    return Result.buildSuccess(count, list);
  }

   
  @Override
  public List<DictionaryDO> queryDictionary(String tableName, String attributeName) {
    DictionaryQuery  query =new DictionaryQuery();
    query.setTableName(tableName);
    query.setAttributeName(attributeName); 
    Result<List<DictionaryDO>> list = queryDictionary(query) ;
    if(list.isFailed())
       return null ;
    return  list.getData() ;
  }
  
  @Override
  public List<DictionaryDO>  queryChildDictionary(String tableName,String attributeName,Integer value){
    DictionaryQuery  query =new DictionaryQuery();
    query.setTableName(tableName);
    query.setAttributeName(attributeName); 
    query.setValue(value);
    List<DictionaryDO>  list = this.dictionaryDAO.queryDictionaryByMoreThanValue(query) ;
    if(list == null || list.isEmpty()){
      List<DictionaryDO> dl = Lists.newArrayList() ;
      DictionaryDO d = queryDictionary(tableName,attributeName,value) ; 
      if(d !=null)
         dl.add(d) ;
      return dl ;
    }
        
    return  list ;
  }
   
  @Override
  public List<DictionaryDO> queryDictionary(String tableName, String attributeName, Long parentId) {
    DictionaryQuery  query =new DictionaryQuery();
    query.setTableName(tableName);
    query.setAttributeName(attributeName); 
    query.setParentId(parentId);
    Result<List<DictionaryDO>> list = queryDictionary(query) ;
    if(list.isFailed())
       return null ;
    return  list.getData() ;
  }
  
  @Override
  public List<DictionaryDO> queryMetaDictionary(String tableName, String attributeName, Long parentId) {
    DictionaryQuery  query =new DictionaryQuery();
    query.setTableName(tableName);
    query.setAttributeName(attributeName); 
    query.setParentId(parentId);
    List<DictionaryDO> list = this.dictionaryDAO.queryMetaDictionary(query) ;
    if(list == null || list.isEmpty())
       return null ;
    return  list  ;
  }

   
  @Override
  public List<DictionaryDO> queryMetaDictionary(String tableName, String attributeName, String code) {
     
    DictionaryQuery  query =new DictionaryQuery();
    query.setTableName(tableName);
    query.setAttributeName(attributeName); 
    query.setCode(code); 
    List<DictionaryDO> list = this.dictionaryDAO.queryMetaDictionary(query) ;
    if(list == null || list.isEmpty())
       return null ;
    return  list  ;
  }
 
  @Override
  public boolean deleteDictionary(Long id) {
    return this.dictionaryDAO.delete(id)  > 0;
  }

  
  @Override
  public DictionaryDO queryDictionary(String tableName, String attributeName, Integer value) {
    Preconditions.checkArgument(tableName !=null && !tableName.equals(""), "tableName:"+tableName+",不能为空");
    Preconditions.checkArgument(attributeName !=null && !attributeName.equals(""), "attributeName:"+attributeName+",不能为空");
    Preconditions.checkArgument(value !=null && value>=0, "value:"+value+",应>=0");
    
    DictionaryQuery  query =new DictionaryQuery();
    query.setTableName(tableName);
    query.setAttributeName(attributeName); 
    query.setValue(value);
    List<DictionaryDO> list  = dictionaryDAO.query(query) ;
    if(list == null || list.isEmpty())
        return null ;
    return list.get(0);
  }
  
  
  

}
